<?php

class Staff_collection extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'Staff_collection':
	 * @var integer $id
	 * @var integer $fk_driver_id
	 * @var double $present_rate
	 * @var double $receipt_rate
	 * @var double $profit_rate
	 * @var double $incentive_amount
	 * @var double $aog_rate
	 * @var double $exception_rate
	 * @var double $attendance_rate
	 * @var integer $accident_times
	 * @var double $amerce
	 * @var string $sanitation_info
	 * @var integer $non_uniform_times
	 * @var integer $fight_times
	 * @var integer $non_obey_times
	 * @var integer $reject_call_times
	 * @var integer $dci_times
	 * @var string $clients_complain_times
	 * @var string $examine_date
	 * @var integer $state
	 * @var string $create_time
	 */
	public $name;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'staff_collection';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('sanitation_info','length','max'=>100),
			array('present_rate,receipt_rate,profit_rate,aog_rate,exception_rate,attendance_rate', 'numerical','integerOnly'=>true,'min'=>0,'max'=>100),
			array('accident_times, non_uniform_times, fight_times, non_obey_times, reject_call_times, dci_times, clients_complain_times', 'numerical', 'integerOnly'=>true),
			array('incentive_amount,amerce', 'numerical'),
			array('amerce,incentive_amount', 'match' ,'pattern' => '/^\d{1,16}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多16位，小数位2位'),
			array('name,present_rate,receipt_rate,profit_rate,incentive_amount,aog_rate,exception_rate,attendance_rate,accident_times,
			amerce,sanitation_info,non_uniform_times,fight_times,non_obey_times,reject_call_times,dci_times,clients_complain_times,examine_date', 'required'),
			array('examine_date','match','pattern' => '/\d{4}-\d{2}$/','message'=>'{attribute} 必须为日期型(年-月)'),
		);
	}

	/**
	 * Prepares attributes before performing validation.
	 */
	protected function beforeValidate($on)
	{
		if($this->isNewRecord){
			$this->state=0;
			$this->create_time=date('Y-m-d G:i:s');
		}
		return true;
	}
	
	protected function afterValidate($on){
		if($this->hasErrors()) return;
		$this->examine_date.='-01';
		$this->present_rate=$this->present_rate/100;
		$this->receipt_rate=$this->receipt_rate/100;
		$this->profit_rate=$this->profit_rate/100;
		$this->aog_rate=$this->aog_rate/100;
		$this->exception_rate=$this->exception_rate/100;
		$this->attendance_rate=$this->attendance_rate/100;
	}
	
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '索引',
			'fk_driver_id' => '司机id',
			'name' => '司机姓名',
			'present_rate' => '到场及时率(%)',
			'receipt_rate' => '回单及时率(%)',
			'profit_rate' => '收入费用比(%)',
			'incentive_amount' => '油耗奖惩(元)',
			'aog_rate' => '到货及时率(%)',
			'exception_rate' => '货损货差率(%)',
			'attendance_rate' => '出勤率(%)',
			'accident_times' => '交通事故次数',
			'amerce' => '罚款(元)',
			'sanitation_info' => '车辆卫生',
			'non_uniform_times' => '不穿工作服次数',
			'fight_times' => '打架斗殴次数',
			'non_obey_times' => '不服从安排次数',
			'reject_call_times' => '拒绝电话次数',
			'dci_times' => '酒后驾车次数',
			'clients_complain_times' => '客户投诉次数',
			'examine_date' => '考核日期',
			'state' => '状态',
			'create_time' => '创建时间',
		);
	}
	
	public function excelAttributes(){//导出excel所需的字段名、列号、宽度
		return array(
			'name'=>array('B',10),
			'examine_date'=>array('C',10),
			'present_rate'=>array('D',15),
			'receipt_rate'=>array('E',15),
			'profit_rate'=>array('F',15),
			'incentive_amount'=>array('G',15),
			'aog_rate'=>array('H',15),
			'exception_rate'=>array('I',15),
			'attendance_rate'=>array('J',15),
			'amerce'=>array('K',10),
		);
	}
	
	public function safeAttributes(){
		$params=parent::safeAttributes();
		$params[]='name';
		return $params;
	}
}